//
//  YJCollectionViewController.m
//  SnackCourier
//
//  Created by qianfeng01 on 15/7/17.
//  Copyright (c) 2015年 qianfeng01. All rights reserved.
//

#import "YJCollectionViewController.h"

#import "DBManager.h"
#import "YJImmediatelygrabControllerModel.h"
#import "Collectioncell.h"

#import "CreateSales.h"

@interface YJCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation YJCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray=[[NSMutableArray alloc] init];
    self.dataArray =[[DBManager sharedManager] readModel];
    [self initcollecView];
}
-(void)initcollecView{
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.collectionView =[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height-64-55) collectionViewLayout:flowLayout];
    self.collectionView.delegate=self;
    self.collectionView.dataSource=self;
    self.collectionView.backgroundColor =[UIColor colorWithWhite:0.2 alpha:1];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"Collectioncell" bundle:nil] forCellWithReuseIdentifier:@"Collectioncell"];
    [self.view addSubview:self.collectionView];
    
}

-(void)viewWillAppear:(BOOL)animated{
    self.dataArray=[[NSMutableArray alloc] init];
    self.dataArray =[[DBManager sharedManager] readModel];
    [self.collectionView reloadData];
}

#pragma mark -

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    Collectioncell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"Collectioncell" forIndexPath:indexPath];
    cell.backgroundColor =[UIColor whiteColor];
    [cell showDataWithIndexPath:indexPath Array:self.dataArray myblock:^(NSMutableArray *arr) {
        self.dataArray =arr;
        [self.collectionView reloadData];
    }];
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((self.view.frame.size.width-10)/2.0, 260);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    YJImmediatelygrabControllerModel *model =self.dataArray[indexPath.row];
    YJImmediatelygrabController *controller =[CreateSales createVC:model.id];
    [self.navigationController pushViewController:controller animated:YES];
}



@end
